package com.ruoyi.basics.controller;

import java.util.List;

import com.ruoyi.basics.domain.User;
import com.ruoyi.basics.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basics.domain.Point;
import com.ruoyi.basics.service.IPointService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.ExcelUtil;

/**
 * 计量点 信息操作处理
 *
 * @author wangbing
 * @date 2018-12-28
 */
@Controller
@RequestMapping("/basics/point")
public class PointController extends BaseController {
    private String prefix = "basics/point";

    @Autowired
    private IPointService pointService;
    @Autowired
    private IUserService userService;

    @RequiresPermissions("basics:point:view")
    @GetMapping()
    public String point() {
        return prefix + "/point";
    }

    /**
     * 查询计量点列表
     */
    @RequiresPermissions("basics:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Point point) {
        startPage();
        List<Point> list = pointService.selectPointList(point);
        return getDataTable(list);
    }


    /**
     * 导出计量点列表
     */
    @RequiresPermissions("basics:user:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Point point) {
        List<Point> list = pointService.selectPointList(point);
        ExcelUtil<Point> util = new ExcelUtil<Point>(Point.class);
        return util.exportExcel(list, "point");
    }

    /**
     * 新增计量点
     */
    @GetMapping("/add/{userId}")
    public String add(@PathVariable("userId") Long userId, ModelMap mmap) {
        User user = userService.selectUserById(userId);
        mmap.put("user", user);
        mmap.put("num", user.getUserNum().split(","));
        return prefix + "/add";
    }

    /**
     * 新增保存计量点
     */
    @RequiresPermissions("basics:user:add")
    @Log(title = "计量点", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Point point) {
        return toAjax(pointService.insertPoint(point));
    }

    /**
     * 修改计量点
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        Point point = pointService.selectPointById(id);
        mmap.put("point", point);
        mmap.put("num", userService.selectUserById(point.getUserId()).getUserNum().split(","));
        return prefix + "/edit";
    }

    /**
     * 修改保存计量点
     */
    @RequiresPermissions("basics:user:edit")
    @Log(title = "计量点", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Point point) {
        return toAjax(pointService.updatePoint(point));
    }

    /**
     * 删除计量点
     */
    @RequiresPermissions("basics:user:remove")
    @Log(title = "计量点", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(pointService.deletePointByIds(ids));
    }

}
